package com.gbb.simple.base;

import android.os.Bundle;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gbb.simple.R;


/**
 * @author gaobingbing
 */
public abstract class TitleFragment extends BaseFragment {

    protected Toolbar mToolbar;

    protected TextView titleView;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.layout_app_base,null);

        mToolbar = (Toolbar)rootView.findViewById(R.id.widget_toolbar);
        mContext.setSupportActionBar(mToolbar);
        titleView = (TextView)rootView.findViewById(R.id.toolbar_title);
        mContext.getSupportActionBar().setDisplayShowTitleEnabled(false);

        onCreateAfter(savedInstanceState);
        initTitleBar();
        return rootView;
    }

    protected abstract void onCreateAfter(Bundle savedInstanceState);

    protected void initTitleBar() {

    }

    public void setTitle(@StringRes int textRes) {
        if(titleView != null) {
            titleView.setText(textRes);
        }
    }

    public void setTitle(CharSequence text) {
        if(titleView != null) {
            titleView.setText(text);
        }
    }

    /**
     * 添加布局文件【默认有标题栏】
     *
     * @param resourceId 布局文件ID
     */
    protected void addLayout(int resourceId) {
        LinearLayout contentLayout = (LinearLayout)rootView.findViewById(R.id.content_layout);
        getActivity().getLayoutInflater().inflate(resourceId,contentLayout);
    }

}
